# -*- coding: utf-8 -*-
'''
@author: olivia.dou
Created on: 2019/9/4 15:27
desc:
'''
import requests
from jsonpath import jsonpath
import base.globalvars as glo

def get_token():
    if glo.get_value("nlp_token") is None:
        url = glo.get_value("url1") + "/csf/nlp-news/public/users/login?username=%s&password=%s"%(glo.get_value("username"),glo.get_value("password"))
        res = requests.post(url)
        assert res.status_code==200,"获取token接口响应代码不为200：%s, %s"%(res.status_code,res.text)
        glo.set_value("nlp_token",res.text)
        return res.text
    else:
        return glo.get_value("nlp_token")

def get_headers():
    need_token = glo.get_value("need_token")
    if need_token == '1':
        token = get_token()
        headers = {"Authorization": "Bearer %s" % token, "Content-Type": "application/json"}
    else:
        headers = {"Content-Type": "application/json"}
    return headers

def submit_content(json):
    """
    提交分析内容（新闻、公告或研报）
    :data:
    :return:
    """
    headers = get_headers()
    url = glo.get_value("url1") + "/csf/nlp-news/api/v1/news/"
    res = requests.post(url,headers=headers,json=json)
    assert res.status_code==200,"提交接口响应代码不为200：%s, %s"%(res.status_code,res.text)
    assert jsonpath(res.json(),"$.newsId") is not False and len(jsonpath(res.json(),"$.newsId"))>0
    newsId = jsonpath(res.json(),"$.newsId")[0]
    glo.set_value("newsId",newsId)
    return newsId


